var targets = {
    type: 0,
    country: 0,
    release: 0,
};
var search_content = $('#search-input').val();
var targets_active = {
    type: $('.types .active'),
    country: $('.country .active'),
    release: $('.release .active'),
};
$(function () {
    // 添加电影
    function add_movie(data) {
        var movie_num = Math.min(data.info.length, 5);
        for (var i = 0; i < movie_num; i++) {
            var row = data.info[i];
            var info = row.information.substring(row.information.indexOf('u3000u3000') + 10, row.information.length);
            // 异步添加HTML代码，加载搜索结果
            var str = '<div class="w3-agileits-news-one">\n' +
                '                    <div class="wthree-news-img">\n' +
                '                        <a href="/movie/single/?movie_id=' + row.id + '"><img src="' + row.url + '" alt=""></a>\n' +
                '                    </div>\n' +
                '                    <div class="wthree-news-info">\n' +
                '                        <h5><a href="/movie/single/?movie_id=' + row.id + '">' + row.movie_name + '</a></h5>\n' +
                '                        <div class="agile-post">\n' +
                '                            评分：<span style="color: #fe423f;">' + row.score + '</span>\n' +
                '                        </div>\n' +
                '                        <div class="agile-post long">\n' +
                '                            导演：' + row.directors + '\n' +
                '                        </div>\n' +
                '                        <div class="agile-post long">\n' +
                '                            演员：' + row.actors + '\n' +
                '                        </div>\n' +
                '                        <div class="too-long">简介：' + info + '</div>\n' +
                '                        <a class="new-more" href="/movie/single/?movie_id=' + row.id + '">了解更多</a>\n' +
                '                    </div>\n' +
                '                    <div class="clearfix"></div>\n' +
                '                </div>';
            $('.latest-news-agile-left-content').append(str);
        }

        // 修改加载更多
        if (movie_num < 5) {
            $('.load_more').css("display", "none");
        }
        // 修改下次请求起始位置
        var cur_movieNum = $('.page_num').text();
        $('.page_num').text(Number(cur_movieNum) + 5);
    }

    // 搜索
    function search(types) {
        // 加载页清零
        $('.page_num').text(0);
        // 显示加载更多按钮
        if ($(".load_more").css('display') == 'none')
            $(".load_more").show();
        $.get('/search_result/', {
            'types': types,
            'content': search_content,
            'targets': JSON.stringify(targets),
        }, function (data) {
            data = JSON.parse(data);
            $('.latest-news-agile-left-content').html("");
            $('.w3ls_head_para').html('已找到' + data.nums + '条结果');
            if (data.status == 200) {
                // 动态添加电影数据
                add_movie(data);
            } else {
                // 404，500状态码处理
                $('.load_more').hide();
            }
        })
    }

    // 加载更多
    function load_more() {
        $.get('/search_result/', {
            'types': 0,
            'content': search_content,
            'targets': JSON.stringify(targets),
            'nums': $('.page_num').text()
        }, function (data) {
            data = JSON.parse(data);
            if (data.status == 200) {
                // 动态添加电影数据
                add_movie(data);
            } else {
                // 404，500状态码处理
            }
        })
    }

    search(0);

    // 异步加载标签
    $.get('/movie/targets/', {}, function (data) {
        data = JSON.parse(data);

        // 加载类型标签
        for (var i=0; i < Math.min(data.info.types.length, 20); i++) {
            var row = data.info.types[i];
            var str = '<li>\n' +
                '            <a href="javascript:void(0);" onclick="">' + row.target + '</a>\n' + '<span style="display: none">' + row.id + '</span>' +
                '      </li>';
            $('.types').append(str);
        }
        $('.types').append('<li><a href="javascript:void(0);">其他</a></li></a><span style="display: none">-1</span>');

        // 加载区域标签
        for (var i=0; i < Math.min(data.info.country.length, 20); i++) {
            var row = data.info.country[i];
            var str = '<li>\n' +
                '            <a href="javascript:void(0);">' + row.target + '</a>\n' + '<span style="display: none">' + row.id + '</span>' +
                '      </li>';
            $('.country').append(str);
        }
        $('.country').append('<li><a href="javascript:void(0);">其他</a></li></a><span style="display: none">-1</span>');

        // 加载年份标签
        for (var i=0; i < Math.min(data.info.release.length, 20); i++) {
            var row = data.info.release[i];
            var str = '<li>\n' +
                '            <a href="javascript:void(0);">' + row.target + '</a>\n' + '<span style="display: none">' + row.id + '</span>' +
                '      </li>';
            $('.release').append(str);
        }

        // 当点击全部按钮，异步刷新
        $('.tags > li').click(function () {
            if (!$(this).hasClass("active")) {
                $(this).addClass("active");
                console.log($(this).parent().hasClass("types"));
                if ($(this).parent().hasClass("types")) {
                    targets_active.type.removeClass("active");
                    targets_active.type = $(this);
                    targets.type = Number($(this).children('span').text());
                } else if ($(this).parent().hasClass("country")) {
                    targets_active.country.removeClass("active");
                    targets_active.country = $(this);
                    targets.country = Number($(this).children('span').text());
                } else {
                    targets_active.release.removeClass("active");
                    targets_active.release = $(this);
                    targets.release = Number($(this).children('span').text());
                }
                // console.log(targets)
                search(0);
            }
        });
    });

    // 当点击搜索按钮，异步刷新
    $('.search').click(function () {
        search_content = $('#search-input').val();
        var types = 0;
        if ($('.picker').text().indexOf("图片") != -1)
            types = 1;

        // 如果在电影搜索页面搜索图片，进行全局搜索
        if (types == 1)
            window.location.href = '/search/?types=' + types + '&content=' + search_content;
        else
            search(types);
    });

    // 当点击加载更多按钮
    $('.load_more').click(function () {
        load_more();
    })
});